﻿using Comany.Core.Extension;
using Company.Core.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Comany.Core.Helper
{
    public static class ImageHelper
    {
        public static Bitmap Load(string path)
        {
            byte[] bytes = FileHelper.Laod(path);
            using (MemoryStream ms = new MemoryStream()) 
            {
                return Image.FromStream(ms) as Bitmap;


            }




        }

        public static UnmanagedArray2D<ushort> LoadTiff(string path)
        {
            if (!File.Exists(path))
            {
                return null;
            }
            Stream stream = new FileStream(path,FileMode.Open, FileAccess.Read,FileShare.Read);
            var decoder=new TiffBitmapDecoder(stream,BitmapCreateOptions.PreservePixelFormat,BitmapCacheOption.Default);
            BitmapSource bitmapSource=decoder.Frames[0];
            int stride = bitmapSource.PixelWidth * bitmapSource.Format.BitsPerPixel / 8;
            ushort[] pixels = new ushort[bitmapSource.PixelWidth*bitmapSource.PixelHeight];
            bitmapSource.CopyPixels(pixels,stride,0);
            var bitmap=bitmapSource.ToBitmap();
            var data = bitmap.LockBits();
            UnmanagedArray2D<ushort> image = new UnmanagedArray2D<ushort>(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
            MemoryHelper.CopyMemory(image.Header,data.Scan0,image.Length);
            bitmap.UnlockBits(data);
            return image;




        }
    }
}
